/*
 * Author: nicolas.bredeche(@)lri.fr
 * Created on 11 d�c. 2006
 * 
 */

package picoevo.tutorials.es.cmaesdemo;

import picoevo.core.evolution.ParameterSet;
import picoevo.es.cmaes.WrapperForCMAES;
import picoevo.toolbox.Misc;

/**
 * @author nicolas
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class CMAESexample {

	public static void main(String[] args) {

		// ### STEP 1 : setting parameters/properties (choose either method 1
		// (load filename) or 2 (manual setting) )

		ParameterSet params = new ParameterSet("CMA-ES ParameterSet");
		params.setProperty("EvaluationOperator", new SphereFunctionEvaluationOperator()); // evaluation
		// operator
		// must
		// be
		// defined
		// as
		// an
		// AbstractObjectiveFunction
		// (nikko
		// hansen's
		// cma-es
		// requirements)

		// ## method 1 (loading properties from file)
		// params.loadProperties("picoevo/tutorials/cmaes/CMAEvolutionStrategy.properties");

		// ## method 2 (setting up properties manually)
		/**/
		// #--- General ---
		params.setProperty("dimension", "20");

		// #--- Boundary, in case lower *and* upper boundary must be set
		// params.setProperty("boundaryLower","-10.0");
		// params.setProperty("boundaryUpper","10.0");

		// #---Initialisation: X and StandardDeviation have precedence
		params.setProperty("initialSearchRegionLower", "-1.0"); // # 1 value or
		// dimension
		// values
		params.setProperty("initialSearchRegionUpper", "0.0"); // # ditto
		// params.setProperty("!initialX","1"); // # 1 value or dimension values
		// params.setProperty("!initialStandardDeviations","0.5"); // # 1 value
		// or dimension values

		// #--- Termination
		params.setProperty("stopFunctionValue", "1e-3");
		params.setProperty("stopTolFun", "1e-12");
		params.setProperty("stopTolFunHist", "1e-12");
		params.setProperty("stopTolX", "0.0"); // # absolute steps
		params.setProperty("stopTolXFactor", "1e-9"); // # relative step-size
		// reduction
		params.setProperty("stopTolUpXFactor", "1000");
		// params.setProperty("stopEvaluations","33");
		// params.setProperty("stopIterations","10");

		// #--- Strategy parameters
		// params.setProperty("populationSize","10"); // # AKA lambda - mu is
		// automaticaly computed as lambda/2

		// #--- Various
		params.setProperty("numberOfRuns", "12"); // restart mode if > 1
		// params.setProperty("lowerStandardDeviations","0"); // # last number
		// is recycled up to dimension
		// params.setProperty("upperStandardDeviations","1"); // # last number
		// is recycled up to dimension
		params.setProperty("maximumTimeFractionForEigendecomposition", "0.5"); // !!!
		// a
		// smaller
		// fraction,
		// e.g.
		// 0.2,
		// may
		// be
		// faster
		// in
		// terms
		// of
		// overall
		// CPU-time:
		/**/

		// ## log files
		String logfilenameprefix = "log/logfile_cmaes_testing_" + Misc.getCurrentTimeAsCompactString();
		params.displayInformation(logfilenameprefix + ".param");
		params.setProperty("outputFileNamesPrefix", logfilenameprefix + "_");

		// ### STEP 2 : INITIALISING and EVOLVING

		WrapperForCMAES myEvolvableWorld = new WrapperForCMAES("EvolvingWithCMA-ES", params);
		myEvolvableWorld.performInitialisation();
		myEvolvableWorld.evolve(); // perform full evolution (with restart, if
		// needed)

		// ### STEP 3 : get the best fitness and related genotype (may also be
		// found in the log files)

		// demo purpose - do what you want
		double bestfitnessvalue = myEvolvableWorld.bestSolution.getFunctionValue();
		double[] bestgenome = myEvolvableWorld.bestSolution.getX();

	}
}
